from django.shortcuts import render
from django.http import JsonResponse,HttpResponse
# Create your views here.
from users.utils.db import db1
import json


def showuseraduit(request):
    sql = "select c.id,c.name,c.tmoney,c.company_name,c.status from customer as c left join reg_form as rf on c.refromid=rf.id left join customer_manager as cm on c.manager_id=cm.id where status=1 or status=3"
    list= db1.findall(sql)
    return JsonResponse({'code':200,'list':list})



def audit(request):

    data = json.loads(request.body)
    print(data)
    import time
    time = int(time.time())
    ssql = "select * from customer where id=%d" % (int(data['id']))
    customer = db1.find(ssql)

    # 更新constmer表，状态，审核人，审核时间
    sql = "update customer set status=%d,audit_name='%s',audit_time=%d where id=%d"%(int(data['is_agree']),'花果山',time,int(data['id']))
    db1.update(sql)

#     更新来源表
    sql1 = "select id,tcount from reg_form where id=%d"%(int(customer['id']))
    res1 = db1.find(sql1)
    num = int(res1['tcount'])+1
    sql2 = "update reg_form set tcount=%d where id=%d"%(num,int(customer['id']))
    db1.update(sql2)

#     更新客户经理中的总金额
    sql3 = "select * from customer_manager where id=%d" % (int(customer['manager_id']))
    manager = db1.find(sql3)
    number = int(manager['tmoney']) + int(customer['tmoney'])

    sql4 = 'update customer_manager set tmoney=%d where id=%d' % (int(number), int(customer['manager_id']))
    db1.update(sql4)
    return JsonResponse({"code": 200})

# 获取注册来源
def getmes(request):
    sql = "select * from reg_form"
    res = db1.findall(sql)
    list = [{'value':i['tcount'],'name':i['name']} for i in res]
    return JsonResponse({'code':200,'list':list})


# 获取客户信息
def getcustomer(request):
    sql = "select * from customer"
    res = db1.findall(sql)
    return JsonResponse({'code':200,'list':res})


from dwebsocket.decorators import accept_websocket
websocketlist = []

@accept_websocket
def websocket_conn(request):
    if request.is_websocket:
        websocketlist.append(request.websocket)
    for message in request.websocket:
        break

# 发送消息
def sendmes(request):
    data = request.POST
    if websocketlist:
        for i in websocketlist:
            mes = [{'name':data['name']}]
            b1= json.dumps(mes,ensure_ascii=False).encode('utf-8')
            i.send(b1)
    return HttpResponse('ok')



# 小说
def addxiaoshuo(request):
    data = json.loads(request.body)
    print(data)
    sql = "insert into xiaoshuo(title,content,status,userid) values('%s','%s',1,%d)"%(data['title'],data['content'],int(data['userid']))
    db1.update(sql)
    return JsonResponse({'code':200})

def getexamine(request):
    sql = "select * from xiaoshuo where status=1"
    list = db1.findall(sql)
    return JsonResponse({'code':200,'list':list})


def examine(request):
    id = request.GET.get('id')
    sql = "select * from xiaoshuo where id=%d"%(int(id))
    res = db1.find(sql)
    return JsonResponse({"code":200,'list':res})

def examineM(request):
    data = json.loads(request.body)
    sql2 = "select * from xiaoshuo where id=%d"%(int(data['id']))
    user = db1.find(sql2)
    rsql = "select * from user where id=%d"%(int(user['userid']))
    res = db1.find(rsql)
    sql3 = 'update xiaoshuo set status=%d where id=%d' % (int(data['is_agree']), int(data['id']))
    db1.update(sql3)
    sql4 = "select * from xiaoshuo where id=%d"%(int(data['id']))
    res4 = db1.find(sql4)
    if int(res4['status'])==2:
        number = int(res['tnumber']) + 2
        sql1 = 'update user set tnumber=%s where id=%d'%(number,user['userid'])
        db1.update(sql1)
    else:
        pass
    return JsonResponse({'code':200})


def showintegral(request):
    sql = "select * from user"
    res = db1.findall(sql)
    list = [{'value': i['tnumber'], 'name': i['username']} for i in res]
    return JsonResponse({'code': 200, 'list': list})